<template>
    <div id="guide">
      <Head />
      <h1>
        <span class="icon">🤖</span> 智能引导
      </h1>
      <div class="content">
        <div class="question-box" v-for="(item, index) in QA" :key="index">
          <div class="question">
            <span class="question-index">{{ index + 1 }}.</span> {{ item.content }}
          </div>
          <div class="answers">
            <label v-for="(answer, i) in item.answers" :key="i" class="answer">
              <input
                type="radio"
                :name="'question_' + index"
                v-model="selectedAnswers[item.id]"
                :value="answer.id"
              />
              <span class="radio-btn"></span>
              {{ answer.content }}
            </label>
          </div>
        </div>
        <button class="submit-btn" @click="submitAnswers">提交</button>
      </div>
  
      <!-- 推荐课程弹框 -->
      <div v-if="showRecommendation" class="recommendation-box" v-loading="recommendedCourses.length === 0">
        <h3>🎯 正在为您推荐...</h3>
        <ul>
          <li v-for="course in recommendedCourses" :key="course.id">
            <a href="javascript:void(0);" @click="resourcesId(course.id)">{{ course.title }}</a>
          </li>
        </ul>
        <button class="close-btn" @click="showRecommendation = false">关闭</button>
      </div>
    </div>
  </template>
    
  <script>
  import Head from "../../components/head.vue";
  
  export default {
    name: "Guide",
    components: { Head },
    data() {
      return {
        QA: [],
        selectedAnswers: {}, // 存储选中的答案 ID
        showRecommendation: false, // 控制推荐框显示
        recommendedCourses: [], // 存储推荐的课程
      };
    },
    methods: {
      getAllQa() {
        this.$http.get("/api/guide/questions-with-answers").then((res) => {
          console.log(res.data.data);
          
          this.QA = res.data.data;
        });
      },
      submitAnswers() {
        const selectedIds = Object.values(this.selectedAnswers);
        if (selectedIds.length !== this.QA.length) {
          this.$message.error("请回答所有问题");
          return;
        }
        let url = "/api/guide/resources?answerIds=" + selectedIds;
        this.$http.get(url).then((res) => {
          if(res.data.data.length === 0) {
            this.$message.warning("暂无推荐课程");
            return;
          }
          this.recommendedCourses = res.data.data;
        });
        
        
        this.showRecommendation = true;
      },

      resourcesId (id) {
        this.$router.push({
        path: "/play",
        query: {
          resourcesId: id,
        },
      });
      }

    },
    mounted() {
      this.getAllQa();
    }
  };
  </script>
    
  <style lang="less" scoped>
  #guide {
    font-family: Arial, sans-serif;
    text-align: center;
  }
  
  h1 {
    margin-top: 20px;
    color: #0056b3;
    font-size: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
  }
  
  .content {
    max-width: 600px;
    margin: 0 auto;
  }
  
  .question-box {
    border: 1px solid #ddd;
    padding: 15px;
    margin-bottom: 15px;
    border-radius: 10px;
    background: #f9f9f9;
    box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.1);
  }
  
  .question {
    font-size: 18px;
    font-weight: bold;
    margin-bottom: 10px;
  }
  
  .answer {
    display: flex;
    align-items: center;
    background: #fff;
    padding: 10px;
    border: 1px solid #ccc;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.3s;
    position: relative;
    font-size: 16px;
  }
  
  .answer:hover {
    background: #e6f0ff;
  }
  
  .answer input {
    display: none;
  }
  
  .radio-btn {
    width: 16px;
    height: 16px;
    border: 2px solid #007bff;
    border-radius: 50%;
    display: inline-block;
    margin-right: 10px;
    position: relative;
  }
  
  .answer input:checked + .radio-btn::after {
    content: "";
    width: 10px;
    height: 10px;
    background: #007bff;
    border-radius: 50%;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
  }
  
  .submit-btn {
    width: 100%;
    padding: 12px;
    background: #007bff;
    color: white;
    font-size: 16px;
    font-weight: bold;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    margin-top: 15px;
  }
  
  .submit-btn:hover {
    background: #0056b3;
  }
  
  .recommendation-box {
    position: fixed;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    background: white;
    padding: 20px;
    border-radius: 10px;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
    width: 300px;
    text-align: center;
  }
  
  .recommendation-box h3 {
    font-size: 18px;
    margin-bottom: 10px;
  }
  
  .recommendation-box ul {
    list-style: none;
    padding: 0;
  }
  
  .recommendation-box li {
    margin: 10px 0;
  }
  
  .recommendation-box a {
    text-decoration: none;
    color: #007bff;
    font-weight: bold;
  }
  
  .recommendation-box a:hover {
    text-decoration: underline;
  }
  
  .close-btn {
    margin-top: 10px;
    padding: 5px 10px;
    background: #ff4d4f;
    color: white;
    border: none;
    border-radius: 5px;
    cursor: pointer;
  }
  
  .close-btn:hover {
    background: #d9363e;
  }
  /deep/ .head_center {
  display: none;
}
/deep/ .header {
  height: 50px;
}
  </style>
  